﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "Login";
    Layout = "~/Views/_ViewImports.cshtml";
}

<div class="layui-row layui-bg-green">
    <div class="layui-col-xs12 layui-col-sm12 layui-col-md4 layui-col-md-offset4" style="height:100%">
        <div class="layui-row">
            <div class="layui-col-md10" style="height:20%"></div>
            <div class="layui-col-xs12 layui-col-sm12 layui-col-md10">
                <div class="layui-card">
                    <div class="layui-card-header"><h2 style="text-align:center">烂笔头·登录</h2></div>
                    <div class="layui-card-body">
                        <form class="layui-form" >
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-font-black">用户名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="userName" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-font-black">密码</label>
                                <div class="layui-input-block">
                                    <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login" >登录</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form','layer','comment'], function () {
        var form = layui.form,
            $ = layui.$,
            comment = layui.comment;

        //如果已经登录过了，就不用登录了
        var token = window.localStorage.getItem("token");
        if(token != null && token.length > 0){
            comment._ACP(ApiUrl.Home.tokenHealthy,{},function(res){
                if(res.success){
                    window.location.href = home;
                }
            });
        }

        //登录
        form.on('submit(login)', function (data) {
            var index = layer.load(1);
            comment._ACP(ApiUrl.Login.Input,data.field,function (res) {
                if(res.success){
                    window.localStorage.setItem("token",'Bearer ' + res.result);
                    window.location.href = "/home/layout"
                }else{
                    //关闭
                    layer.close(index);
                    layer.alert(res.errorMsg,{icon:2});
                }
            },true)

            return false;//禁止跳转
        });

    })
</script>